package offer;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * 描述:剑指 Offer 32 - II. 从上到下打印二叉树 II
 *
 * @author Madison You
 * @created 0:22
 */
public class Easy_32_LevelOrder {
    public static void main(String[] args) {

    }
    public List<List<Integer>> levelOrder(TreeNode root) {
        ArrayList<List<Integer>> lists = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if (root == null) return lists;
        queue.offer(root);
        while (!queue.isEmpty()) {
            ArrayList<Integer> integers = new ArrayList<>();
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                integers.add(node.val);
                if (node.left!=null) queue.offer(node.left);
                if (node.right!=null) queue.offer(node.right);
            }
            lists.add(integers);
        }
        return lists;
    }

}
